package com.hxb.example;

import com.hxb.example.entity.BaseEntity;
import com.hxb.example.entity.SysDictData;
import com.hxb.example.listener.flex.MyInsertListener;
import com.hxb.example.listener.flex.MyUpdateListener;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @Description MyBatisFlex配置 注册自定义主键 审计 缓存 SQL日志打印
 * @since hxb
 * @Date 2024-03-28
 */
@Slf4j
@Component
public class MyBatisFlexConfiguration implements MyBatisFlexCustomizer {

    /**
     * 自定义的FlexConfiguration配置
     */
    @Override
    public void customize(FlexGlobalConfig globalConfig) {
        globalConfig.setPrintBanner(false);//不打印banner
        globalConfig.registerInsertListener(new MyInsertListener(), BaseEntity.class);
        globalConfig.registerUpdateListener(new MyUpdateListener(), BaseEntity.class);

        //开启审计功能
        AuditManager.setAuditEnable(true);

        //设置 SQL 审计收集器
        AuditManager.setMessageCollector(auditMessage ->
                log.info("\r\n执行sql: {}\r\n执行用时: {}ms", auditMessage.getFullSql(), auditMessage.getElapsedTime()));
    }
}